package hlhj.fhp.checkpoint.atys

import android.content.res.ColorStateList
import android.graphics.Rect
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.os.Handler
import android.support.annotation.RequiresApi
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.RadioButton
import android.widget.RadioGroup
import com.example.mymvp.log
import com.liaoinstan.springview.utils.DensityUtil
import hlhj.fhp.checkpoint.BaseAty
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.customView.ChooseLocationPop
import hlhj.fhp.checkpoint.customView.ChooseTypeDialog
import hlhj.fhp.checkpoint.iview.AddCarView
import hlhj.fhp.checkpoint.javaBean.*
import hlhj.fhp.checkpoint.presenter.AddCarPresenter
import hlhj.fhp.checkpoint.utils.ToastUtils
import kotlinx.android.synthetic.main.aty_addcar.*
import kotlinx.android.synthetic.main.lo_pub_head.*
import org.greenrobot.eventbus.EventBus
import android.view.ViewTreeObserver
import android.view.View.FOCUS_DOWN
import hlhj.fhp.checkpoint.customView.AndroidBug5497Workaround


/**
 * Created by Never Fear   on 2018\11\8 0008.
Never More....
 */
class AddCarAty:BaseAty<AddCarView,AddCarPresenter>(), AddCarView, ChooseTypeDialog.ClickResultListener {
    private var choosePop:ChooseLocationPop?=null
    private var chooseTypeDialog:ChooseTypeDialog?=null
    private var carType=""
    private val ID=1226
    private var playType=0
    private var city=""
    private var cityId=0
    private var c_id="0"
    override fun click(type: String) {
        carType=type
        if (type.equals("01")){
            btType.text="大车"
        }else{
            btType.text="小车"
        }
    }

    //限制回调
    override fun setLimit(limitBean: LimitBean) {
        try {
            city=limitBean.info.city
            cityId=limitBean.info.id
            c_id=limitBean.info.city_id
            when(limitBean?.info.engineNoLength){
                "-1"->{
                    etEngin.setHint("请输入发动机号")
                }
                "0"->{
                    etEngin.setHint("非必须")
                }
                else->{
                    etEngin.setHint("请输入发动机号后${limitBean?.info.engineNoLength}位")
                }
            }
            when(limitBean?.info.vinLength){
                "-1"->{
                    etVin.setHint("车身架号")
                }
                "0"->{
                    etVin.setHint("非必须")
                }
                else->{
                    etVin.setHint("请输入车身架号后${limitBean?.info.vinLength}位")
                }
            }
            when(limitBean?.info.registno){
                "0"->{
                    etBookNum.setHint("非必须")
                }
                "-1"->{
                    etBookNum.setHint("请输入证书编号")
                }
                else->{
                    etBookNum.setHint("请输入证书编号后${limitBean?.info.registno}位")
                }
            }
            chooseTypeDialog= ChooseTypeDialog(this,this, limitBean?.info.carTypes as ArrayList<String>)


        }catch (e:Exception){
            e.printStackTrace()
        }

    }

    //地区缩写回调
    override fun setCitys(locationBean: LocationBean) {

        choosePop?.showAtLocation(father,Gravity.CENTER,0,0)
        val params = window.attributes
        params.alpha=0.5f
        window.attributes=params
        choosePop?.refresh(locationBean.info as ArrayList<LocationBean.InfoBean>)
    }

    //添加结果回调
    override fun setAddResult(myBaseBean: MyBaseBean) {
        EventBus.getDefault().post(RefreshCarEvent())
        ToastUtils.toast("添加成功",this)
        finish()
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun setAllPlate(plateBean: PlateBean) {
        plateBean.info.forEachIndexed { index, infoBean ->
            val radioButton = RadioButton(this)
            radioButton.buttonDrawable=null
            radioButton.setText(infoBean.name)
            radioButton.id=1226+index
            radioButton.gravity=Gravity.CENTER
            val stateList = resources.getColorStateList(R.color.addcar_text_sele_color)
            radioButton.setTextColor(stateList)
            val lp = RadioGroup.LayoutParams(DensityUtil.dip2px(this, 66f), DensityUtil.dip2px(this, 34f))
            lp.marginEnd=DensityUtil.dip2px(this,10f)
            radioButton.layoutParams=lp

            if (infoBean.name.contains("黄")){
                val drawable = resources.getDrawable(R.drawable.yellow_sele)
                radioButton.background=drawable
            }else if (infoBean.name.contains("蓝")){
                val drawable = resources.getDrawable(R.drawable.blue_sele)
                radioButton.background=drawable
            }else if (infoBean.name.contains("绿")){
                val drawable = resources.getDrawable(R.drawable.green_sele)
                radioButton.background=drawable
            }
            rgPlate.addView(radioButton)
        }

        rgPlate.setOnCheckedChangeListener { group, checkedId ->
            log("fhp","选中ID为${checkedId}")
            playType=plateBean.info[checkedId-ID].id
        }
    }

    override fun initView() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
        AndroidBug5497Workaround.assistActivity(this)

        tvTittle.text="添加车辆"
        choosePop=ChooseLocationPop(this)
    }



    override fun logicStart() {
        presenter?.getAllPlate(this)
    }

    override fun setListener() {
        choosePop?.clickListener=object :ChooseLocationPop.OnTagClickListener{
            override fun click(name: String, fStr: String, sStr: String) {
                btChoose.text=name
                presenter?.getLimit(fStr,sStr,this@AddCarAty)
            }
        }
        choosePop?.setOnDismissListener {
            val params = window.attributes
            params.alpha=1f
            window.attributes=params
        }

        btChoose.setOnClickListener {
            presenter?.getCity(this)

        }

        btType.setOnClickListener {
            if (chooseTypeDialog==null){
                ToastUtils.toast("请先填写完成车牌号码",this)
                return@setOnClickListener
            }
            chooseTypeDialog?.show()
//            setResult(-1)
//            finish()
        }

        btCommit.setOnClickListener {
            presenter?.addCar(cityId,"${btChoose.text.toString()}${etCarNum.text.toString()}",etVin.text.toString(),etEngin.text.toString(),carType
            ,etNanme.text.toString(),etPhone.text.toString(),playType.toString(),city,this,etBookNum.text.toString(),c_id)
        }
    }

    override fun bindView(): AddCarView {
        return this
    }

    override fun createPresenter(): AddCarPresenter {
        return AddCarPresenter()
    }

    override val contentId: Int= R.layout.aty_addcar
}